<template>
    <div :class="{'platform':loginForm.role===1, 'login':loginForm.role===0}">
    <div class="login-box">
<!--      <img src="@/assets/login/login-l.png" alt="" />-->
      <div class="login-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <div class="login-form-title">
            <img
              :class="{'shopImage':loginForm.role===0, 'platformImage':loginForm.role===1}"
              alt="" :src="loginForm.role===0?require('../../assets/login/wang.png'):require('../../assets/login/puff.png')"/>
          </div>
          <el-form-item prop="username" style="color: white">
            <label style="color: white">用户：
            <el-input
              style="width: 150px;border-radius: 10px"
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
              prefix-icon="iconfont icon-user"
            />
            </label>
          </el-form-item>
          <el-form-item prop="password">
            <label style="color: white">密码：
            <el-input
              style="width: 150px"
              v-model="loginForm.password"
              type="password"
              show-password
              placeholder="密码"
              prefix-icon="iconfont icon-lock"
              @keyup.enter.native="handleLogin"
            />
            </label>
          </el-form-item>
          <el-form-item prop="role">
            <el-radio-group v-model="loginForm.role" style="margin-left: 20px;vertical-align: center">
              <el-radio :label="0"><span style="color: white">管理端</span></el-radio>
              <el-radio :label="1"><span style="color: white">平台端</span></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item style="width: 100%;text-align: center">
            <el-button
              :loading="loading"
              class="login-btn"
              size="medium"
              type="primary"
              style="width: 70%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import { Route } from 'vue-router'
import { Form as ElForm, Input } from 'element-ui'
import { UserModule } from '@/store/modules/user'
import { isValidUsername } from '@/utils/validate'
import {SET_ROLE} from "@/utils/localStorage";
import {login} from "@/api/employee";

@Component({
  name: 'Login',
  methods: {login},
})
export default class extends Vue {
  private validateUsername = (rule: any, value: string, callback: Function) => {
    if (!value) {
      callback(new Error('请输入用户名'))
    } else {
      callback()
    }
  }

  @Watch('loginForm.role')
  onChangeValue(newVal, oldVal) {
    if(newVal===0) {
      this.loginForm.username='admin'
    }else {
      this.loginForm.username='puff'
    }
  }

  private validatePassword = (rule: any, value: string, callback: Function) => {
    if (value.length < 6) {
      callback(new Error('密码必须在6位以上'))
    } else {
      callback()
    }
  }
  private loginForm = {
    username: 'admin',
    password: '123456',
    role:0
  } as {
    username: String
    password: String
    role:Number
  }

  loginRules = {
    username: [{ validator: this.validateUsername, trigger: 'blur' }],
    password: [{ validator: this.validatePassword, trigger: 'blur' }],
  }
  private loading = false
  private redirect?: string

  @Watch('$route', { immediate: true })
  private onRouteChange(route: Route) {}

  // 登录
  private handleLogin() {
    (this.$refs.loginForm as ElForm).validate(async (valid: boolean) => {
      if (valid) {
        this.loading = true
        await UserModule.Login(this.loginForm as any)
          .then((res: any) => {
            if (String(res.code) === '1') {
              UserModule.GET_USERROLE(res.data.role)
              // role的值存在localstorage
              // if(!res.data.role) {
              //   SET_ROLE()
              // }
              console.log("login",res)
              this.$router.push('/')
            } else {
              // this.$message.error(res.msg)
              this.loading = false
            }
          })
          .catch(() => {
            // this.$message.error('用户名或密码错误！')
            this.loading = false
          })
      } else {
        return false
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  //opacity: 0;
  // background: #476dbe;
  //background: linear-gradient(to right, #65cbf7, #B3a5fc);
  background: url("../../assets/login/shop.jpg") no-repeat;
  background-size: cover;
  transition: all 0.3s;
  &:active{
    opacity: 1;
  }
}

.hcqFont {
  letter-spacing: .07em;
  font-size: 3em;
  font-weight: normal;
  margin: 0 auto;
}

.platform {
  //opacity: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  // background: #476dbe;
  //background: linear-gradient(to right, #65cbf7, #B3a5fc);
  background: url("../../assets/login/platform.png") no-repeat;
  background-size: cover;
  transition: all 0.3s;
  &:active {
    opacity: 1;
  }
}

.login-box {
  width: 400px;
  height: 474.38px;
  border-radius: 8px;
  display: flex;
  //img {
  //  width: 60%;
  //  height: auto;
  //}
}



.title {
  margin: 0px auto 10px auto;
  text-align: left;
  color: #707070;
}

.login-form {
  //background: #ffffff;
  backdrop-filter: blur(7px);
  border: 2px solid rgba(255,255,255,1);
  //border-right: 2px solid rgba(255,255,255,0.5);
  //border-left: 2px solid rgba(255,255,255,0.5);
  //border-bottom: 2px solid rgba(255,255,255,0.5);
  width: 400px;
  border-radius: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    width: 214px;
    height: 307px;
  }
  .el-form-item {
    margin-bottom: 30px;
  }
  ::v-deep .el-input__inner {
    //border: 0 !important;
    //border-bottom: 1px solid #fd7065 !important;
    border-radius: 10px;
    //border: 2px solid rgba(255,255,255,0.5);
    opacity: 0.7;
    //background: #fff !important;
  }

  ::v-deep .el-input.el-input--prefix {
    //opacity: 0;
    //height: 60px;
    //border: 2px solid rgba(255,255,255,0.5);
    border-radius: 20px;
  }

  ::v-deep .el-input.el-input--prefix.el-input__suffix {
    //border: 2px solid rgba(255,255,255,0.5);
    border-radius: 20px;
  }

  .input-icon {
    height: 32px;
    width: 18px;
    margin-left: -2px;
  }
  .el-input__inner {
    border: 0;
    border-bottom: 1px solid #e9e9e8;
    border-radius: 0;
    font-size: 12px;
    font-weight: 400;
    color: #333333;
    height: 32px;
    line-height: 32px;
  }
  .el-input__prefix {
    left: 0;
  }
  .el-input--prefix .el-input__inner {
    padding-left: 26px;
  }
  .el-input__inner::placeholder {
    color: #aeb5c4;
  }
  .el-form-item--medium .el-form-item__content {
    line-height: 32px;
  }
  .el-input--medium .el-input__icon {
    line-height: 32px;
  }
}

.login-btn {
  border-radius: 17px;
  padding: 11px 20px !important;
  margin-top: 10px;
  font-weight: 500;
  font-family: inherit;
  font-size: 14px;
  border: 0;
  font-weight: 500;
  color: #333333;
  // background: #09a57a;
  background-color: #e8e8e7;
  &:hover {
    cursor: pointer;
    animation: jelly 0.5s;
  }
  @keyframes jelly {
    0%,
    100% {
      transform: scale(1, 1);
    }
    25% {
      transform: scale(0.9, 1.1);
    }
    50% {
      transform: scale(1.1, 0.9);
    }
    75% {
      transform: scale(0.95, 1.05);
    }
  }
  &:focus {
    // background: #09a57a;
    background-color: #e8e8e7;
    color: #ffffff;
  }
}
.login-form-title {
  height: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 40px;
  .shopImage {
    position: relative;
    //background: url("../../assets/login/wang.png") no-repeat;
    width: 280px;
    height: 130px;
    top: -20px;
  }
  .platformImage {
    position: relative;
    top: -20px;
    width: 195px;
    height: 130px;
    //background: url("../../assets/login/puff.png") no-repeat;
  }
}
</style>
